package com.bilibili.config;

import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.elasticsearch.client.ClientConfiguration;
import org.springframework.data.elasticsearch.client.RestClients;
import org.springframework.data.elasticsearch.config.AbstractElasticsearchConfiguration;

/**
 * projectName:com.bilibili.config
 *
 * @author 飞
 * @create by 2024/6/1816:37
 * description:
 */
/**
 * ElasticSearch配置类，继承自AbstractElasticsearchConfiguration，用于配置ElasticSearch客户端。
 */
@Configuration
public class ElasticSearchConfig extends AbstractElasticsearchConfiguration {

    /**
     * 从配置文件中读取ElasticSearch的URL地址。
     */
    @Value("${elasticsearch.url}")
    private String esUrl;

    /**
     * 创建并配置ElasticSearch的RestHighLevelClient。
     *
     * @return 配置好的RestHighLevelClient实例。
     */
    @Bean
    @Override
    public RestHighLevelClient elasticsearchClient() {
        // 构建客户端配置，指定连接到ElasticSearch的URL
        final ClientConfiguration clientConfiguration = ClientConfiguration.builder()
                .connectedTo(esUrl)
                .build();
        // 根据配置创建并返回RestHighLevelClient实例
        return RestClients.create(clientConfiguration).rest();
    }
}

